/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.isfd.controller;

import java.util.List;

import com.comac.ins.isfd.domain.bo.IsfdMetadataBo;
import com.comac.ins.isfd.domain.vo.IsfdMetadataVo;
import com.comac.ins.isfd.service.IIsfdMetadataService;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.comac.ins.common.idempotent.annotation.RepeatSubmit;
import com.comac.ins.common.log.annotation.Log;
import com.comac.ins.common.web.core.BaseController;
import com.comac.ins.common.mybatis.core.page.PageQuery;
import com.comac.ins.common.core.domain.R;
import com.comac.ins.common.core.validate.AddGroup;
import com.comac.ins.common.core.validate.EditGroup;
import com.comac.ins.common.log.enums.BusinessType;
import com.comac.ins.common.excel.utils.ExcelUtil;
import com.comac.ins.common.mybatis.core.page.TableDataInfo;

/**
 * 元数据信息
 *
 * @author Lion Li
 * @date 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/metadata")
public class IsfdMetadataController extends BaseController {

    private final IIsfdMetadataService isfdMetadataService;

    /**
     * 查询元数据信息列表
     */
    @SaCheckPermission("system:metadata:list")
    @GetMapping("/list")
    public TableDataInfo<IsfdMetadataVo> list(IsfdMetadataBo bo, PageQuery pageQuery) {
        return isfdMetadataService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出元数据信息列表
     */
    @SaCheckPermission("system:metadata:export")
    @Log(title = "元数据信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IsfdMetadataBo bo, HttpServletResponse response) {
        List<IsfdMetadataVo> list = isfdMetadataService.queryList(bo);
        ExcelUtil.exportExcel(list, "元数据信息", IsfdMetadataVo.class, response);
    }

    /**
     * 获取元数据信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("system:metadata:query")
    @GetMapping("/{id}")
    public R<IsfdMetadataVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(isfdMetadataService.queryById(id));
    }

    /**
     * 新增元数据信息
     */
    @SaCheckPermission("system:metadata:add")
    @Log(title = "元数据信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IsfdMetadataBo bo) {
        return toAjax(isfdMetadataService.insertByBo(bo));
    }

    /**
     * 修改元数据信息
     */
    @SaCheckPermission("system:metadata:edit")
    @Log(title = "元数据信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IsfdMetadataBo bo) {
        return toAjax(isfdMetadataService.updateByBo(bo));
    }

    /**
     * 删除元数据信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("system:metadata:remove")
    @Log(title = "元数据信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(isfdMetadataService.deleteWithValidByIds(List.of(ids), true));
    }
}
